package com.bingbing.back.service.Impl;

import com.bingbing.back.dto.SchoolInfoDto;
import com.bingbing.back.dto.SchoolMajorScoreDto;
import com.bingbing.back.dto.VoluntaryDto;
import com.bingbing.back.mapper.RecruitPlanMapper;
import com.bingbing.back.mapper.SchoolMajorScoreMapper;
import com.bingbing.back.mapper.VoluntaryMapper;
import com.bingbing.back.myUtils.MyCalProbability;
import com.bingbing.back.myUtils.MySort;
import com.bingbing.back.po.RecruitPlan;
import com.bingbing.back.po.SchoolMajorScore;
import com.bingbing.back.service.VoluntaryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class VoluntaryServiceImpl implements VoluntaryService {

    @Resource
    private VoluntaryMapper voluntaryMapper;
    @Resource
    private MyCalProbability myCalProbability;
    @Resource
    private RecruitPlanMapper recruitPlanMapper;
    @Resource
    private MySort mySort;

    @Override
    public List<SchoolMajorScoreDto> voluntaryMajor(int type, VoluntaryDto dto) {
        List<SchoolMajorScoreDto> result = new ArrayList<>();
        String planYear = voluntaryMapper.getLastestYearPlan();

        int majorYear = voluntaryMapper.getLastestYearMajor();
        dto.setYear(majorYear);
        List<SchoolMajorScoreDto> list = voluntaryMapper.voluntaryMajor(dto);

        RecruitPlan recruitPlan = new RecruitPlan();
        for (SchoolMajorScoreDto s : list) {
            recruitPlan.setSchoolId(s.getSchoolId());
            recruitPlan.setMajorName(s.getMajorName());
            recruitPlan.setPlanYear(planYear);
            List<RecruitPlan> listPlan = recruitPlanMapper.searchRecruitPlan(recruitPlan);

            if(listPlan.isEmpty() == false){
                int total = recruitPlanMapper.getTotal(s.getSchoolId(), planYear);
                RecruitPlan rp = listPlan.get(0);
                s.setRecruitPlan(rp);
                s.setProbability(myCalProbability.calMajorProbability(total, dto, s));

                double Pro = s.getProbability();
//                System.out.println(s.getSchoolName() + "\t" + s.getMajorName() + "\t" + Pro);
                if(Double.compare(5.0, Pro) > 0 || Double.compare(99.0, Pro) < 0){
                    continue;
                }
                if(type == 0){
                    result.add(s);
                } else if (type == 1 && Double.compare(35.0, Pro) >= 0){
                    result.add(s);
                } else if (type == 2 && Double.compare(60.0, Pro) >= 0 && Double.compare(35.0, Pro) < 0) {
                    result.add(s);
                } else if(type == 3 && Double.compare(60.0, Pro) < 0) {
                    result.add(s);
                }
            }
        }
        list = mySort.sortByMajorProbability(result);
        return list;
    }
}
